package com.css.bjdt_core.report.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.nutz.dao.Cnd;
import org.nutz.dao.Condition;
import org.nutz.dao.Dao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.css.bjdt_core.cell.entity.Cell;
import com.css.bjdt_core.cell.service.CellService;
import com.css.bjdt_core.report.entity.RptTemplateCondition;
import com.css.bjdt_core.report.entity.RptTemplateCondition.ConditionType;
import com.css.bjdt_core.report.entity.ViewRptTemplateCondition;
import com.css.bjdt_core.report.entity.ViewRptTemplateConditionShow;
import com.css.bjdt_core.util.CommonUtil;
import com.css.bjdt_core.util.PageQuerys;

@Service
public class ReportConditionService {
	@Autowired
	private Dao dao;

	@Autowired
	private CellService cellService;

	public RptTemplateCondition insert(RptTemplateCondition record) {
		if (record == null) {
			return null;
		}
		if (StringUtils.isBlank(record.getConditionId())) {
			record.setConditionId(CommonUtil.getUUID());
		}
		return dao.insert(record);
	}

	public void insertList(List<RptTemplateCondition> rptTemplateConditionList) {
		for (RptTemplateCondition rptTemplateCondition : rptTemplateConditionList) {
			insert(rptTemplateCondition);
		}
	}

	public boolean batchInsert(List<RptTemplateCondition> records) {
		if (records == null || records.size() == 0) {
			return false;
		}
		for (RptTemplateCondition condition : records) {
			if (StringUtils.isBlank(condition.getConditionId())) {
				condition.setConditionId(CommonUtil.getUUID());
			}
		}
		dao.insert(records);
		return true;
	}

	public RptTemplateCondition getARptTemplateCondition(String recordId) {
		return dao.fetch(RptTemplateCondition.class, Cnd.where("CONDITION_ID", "=", recordId));
	}

	/**
	 * 更新条件
	 * @param record
	 * @return
	 */
	public int update(RptTemplateCondition record) {
		return dao.updateIgnoreNull(record);
	}

	//删除单个查询条件
	public int delete(String recordId) {
		return dao.delete(RptTemplateCondition.class, recordId);
	}

	//删除总控条件、行条件、列条件
	public int deleteTotalRowCol(String templateId) {
		return dao.clear(RptTemplateCondition.class,
				Cnd.where("TEMPLATE_ID", "=", templateId).and("CONDITION_TYPE", "in", "1,2,3"));
	}

	//删除总控条件、行条件、列条件
	public int deleteCellRowCol(String templateId, String row, String col) {
		return dao.clear(RptTemplateCondition.class, Cnd.where("TEMPLATE_ID", "=", templateId)
				.and("CONDITION_TYPE", "=", "4").and("ABS_ROW_NUM", "=", row).and("ABS_COL_NUM", "=", col));
	}

	public int clear(List<String> recordIds) {
		return dao.clear(RptTemplateCondition.class, Cnd.where("CONDITION_ID", "IN", recordIds));
	}

	public List<RptTemplateCondition> getConditionByTemplateId(String templateId) {
		if (StringUtils.isBlank(templateId)) {
			return null;
		}
		return dao.query(RptTemplateCondition.class, Cnd.where("TEMPLATE_ID", "=", templateId));
	}

	//根据条件类型，查询出相关条件集合
	public List<ViewRptTemplateCondition> getConditionByType(String templateId, ConditionType type) {
		return dao.query(ViewRptTemplateCondition.class,
				Cnd.where("TEMPLATE_ID", "=", templateId).and("CONDITION_TYPE", "=", type.getKey()).asc("ORDER_ID"));
	}

	/**
	 * 分页查询条件
	 * @param cnd
	 * @return
	 */
	public Map getConditionByTypeLigerUi(Condition cnd, int pageNum, int pageSize) {
		Map result = PageQuerys.ligerGridQuery(dao, ViewRptTemplateConditionShow.class, cnd, pageNum, pageSize);
		return result;
	}

	//根据行列条件查询出当前的单元格条件
	public List<ViewRptTemplateCondition> getCellConditionByType(String templateId, ConditionType type, String row,
			String col) {
		return dao.query(ViewRptTemplateCondition.class,
				Cnd.where("TEMPLATE_ID", "=", templateId).and("CONDITION_TYPE", "=", type.getKey())
						.and("ABS_ROW_NUM", "=", row).and("ABS_COL_NUM", "=", col).asc("ORDER_ID"));
	}

	public Map<String, List<ViewRptTemplateCondition>> buildConditionMapByTemplateId(String templateId) {
		Map<String, List<ViewRptTemplateCondition>> conditionMap = new HashMap<String, List<ViewRptTemplateCondition>>();
		List<ViewRptTemplateCondition> totalConditions = getConditionByType(templateId, ConditionType.total);
		conditionMap.put(ConditionType.total.getKey(), totalConditions);
		List<ViewRptTemplateCondition> rowConditions = getConditionByType(templateId, ConditionType.row);
		conditionMap.put(ConditionType.row.getKey(), rowConditions);
		List<ViewRptTemplateCondition> colConditions = getConditionByType(templateId, ConditionType.col);
		conditionMap.put(ConditionType.col.getKey(), colConditions);
		List<ViewRptTemplateCondition> cellConditions = getConditionByType(templateId, ConditionType.cell);
		conditionMap.put(ConditionType.cell.getKey(), cellConditions);
		return conditionMap;
	}

	//根据条件类型和行列号，查询出相关条件集合
	public List<ViewRptTemplateCondition> getConditionByTypeRowOrCol(String templateId, ConditionType type,
			String rowOrCol) {
		if (type.getKey().equals("2")) {
			return dao.query(ViewRptTemplateCondition.class, Cnd.where("TEMPLATE_ID", "=", templateId)
					.and("CONDITION_TYPE", "=", "2").and("REL_ROW_NUM", "=", rowOrCol).asc("ORDER_ID"));
		} else if (type.getKey().equals("3")) {
			return dao.query(ViewRptTemplateCondition.class, Cnd.where("TEMPLATE_ID", "=", templateId)
					.and("CONDITION_TYPE", "=", "3").and("REL_COL_NUM", "=", rowOrCol).asc("ORDER_ID"));
		} else {
			return null;
		}
	}

	//单元格条件说明查看(传绝对坐标)
	public String cellConditionInstructions(String templateId, String col, String row) {
		StringBuffer sb = new StringBuffer();
		//先判断当前单元格是表内单元格还是表外单元格
		Cell cell = cellService.getRawCellByTemplateIdRowCol(templateId, row, col);
		//先获得总控条件
		List<ViewRptTemplateCondition> totalConditions = getConditionByType(templateId, ConditionType.total);
		String zongkong = condListToCondStr(totalConditions);
		//先解决表内单元格
		if (cell.getAreaType().equals("1") && cell.getRelColNum() != null && cell.getRelRowNum() != null) {
			List<ViewRptTemplateCondition> rowConditions = getConditionByTypeRowOrCol(templateId, ConditionType.row,
					cell.getRelRowNum() + "");
			String hang = condListToCondStr(rowConditions);
			List<ViewRptTemplateCondition> colConditions = getConditionByTypeRowOrCol(templateId, ConditionType.col,
					cell.getRelColNum() + "");
			String lie = condListToCondStr(colConditions);
			List<ViewRptTemplateCondition> cellConditions = getCellConditionByType(templateId, ConditionType.cell, row,
					col);
			String danyuange = condListToCondStr(cellConditions);
			sb.append(zongkong);
			if (zongkong.length() > 0 && !zongkong.equals("")) {
				sb.append(" 并且 ");
			}
			sb.append(hang);
			if (hang.length() > 0 && !hang.equals("")) {
				sb.append(" 并且 ");
			}
			sb.append(lie);
			if (lie.length() > 0 && !lie.equals("")) {
				sb.append(" 并且 ");
			}
			if (danyuange.length() > 0 && !danyuange.equals("")) {
				sb.append(danyuange);
			}
		}
		//再解决表外单元格
		else {
			List<ViewRptTemplateCondition> cellConditions = getCellConditionByType(templateId, ConditionType.cell, row,
					col);
			String danyuange = condListToCondStr(cellConditions);
			sb.append(zongkong);
			if (zongkong.length() != 0 && !zongkong.equals("")) {
				sb.append(" 并且 ");
			}
			if (danyuange.length() != 0 && !danyuange.equals("")) {
				sb.append(danyuange);
			}
		}
		if (sb.toString().length() < 4) {
			return "";
		} else {
			String returnStr = sb.toString().substring(0, sb.toString().length() - 3);
			return returnStr;
		}

	}

	//把条件集合转换成string字符串
	public String condListToCondStr(List<ViewRptTemplateCondition> viewRptTemplateConditionList) {
		StringBuffer sb = new StringBuffer();
		for (ViewRptTemplateCondition viewRptTemplateCondition : viewRptTemplateConditionList) {
			sb.append(viewRptTemplateCondition.getItemChineseName()).append(" ")
					.append(viewRptTemplateCondition.getRelaOperatorNameChinese()).append(" ")
					.append(viewRptTemplateCondition.getRealConditionValChinese()).append(" ")
					.append(viewRptTemplateCondition.getLogicOperatorChineseName()).append(" ");
		}
		return sb.toString();
	}
}
